/*
RF ESTIMATION ON AGE CUT-OFF

Data 	: B1_age55data.dta.
Folder 	: Jonas/data/B1_age55data.dta
Date	: 2018-03-12

Creator		: Jonas Cederlof	(JC)
Description : 
Notes:


LATEST UPDATE: 	

*/

********************************************************************************

clear
set more		 off
cap   log close 	_all

log using 	"../log/B2_RFestimation.log"	, replace 
use 		 "$datapath/A3_age55data_WC.dta"

keep if inrange(eventtime_def,0,24)

{ // Define globals
*===============================================================================
global covariets 	"C_annual_ear_prenot_def C_female C_immigrant C_tenureatnot C_educ1 C_educ2 C_educ3 "	
global instrument	"treat"
global linear		"runvar inter"
global quadratic	"$linear q_runvar q_inter"
global cubic 		"$quadratic c_runvar c_inter"
}
*

*=========
xtset ym_def
*=========

gen nonemp 	= working2==0 

{ // Annual earnings (t+1) and employment probabilites in m+6
*===============================================================================
*Generate: outcomes
*gen otherfirm2	= atfirm2==0  & working2==1



eststo   clear
eststo : qui xtreg annual_ear 		$instrument $linear $covariets  if eventtime_def==12 , cluster(varselid) fe nonest
eststo : qui xtreg atfirm2		$instrument $linear $covariets  if eventtime_def==6 , cluster(varselid) fe nonest
eststo : qui xtreg otherfirm2	 	$instrument $linear $covariets  if eventtime_def==6 , cluster(varselid) fe nonest
eststo : qui xtreg nonemp	 	$instrument $linear $covariets 	if eventtime_def==6 , cluster(varselid) fe nonest
esttab , keep($instrument _cons) se(3) b(3) star(* 0.1 ** 0.05 *** 0.01)

esttab using "$agetablepath/RF/tab_ear_and_premp.tex", 			/// 
replace keep($instrument _cons) booktabs nodepvars nomtitles  			///
varlabels(treat "Long NT" _cons "Control mean") se(3) b(3)  			///
star(* 0.1 ** 0.05 *** 0.01) refcat( _cons "", nolabel)   			///
stats(r2  N_clust N, fmt( %9.3f %9.0g %9.0g) 					/// 
labels( "$ R^2 $"  "No. of Clusters" "N")) 					///
title("\textsc{RD-estimates on earnings and employment} " ) 			///
mlabels("Earnings (t+1)" " $ \Pr(At firm \mid m=6 ) $ " " $ \Pr(Other firm \mid m=6 ) $" " $ \Pr(NE\mid m=6 ) $"    ) 					///
nonotes 


}
*
{ // Annual earnings (t+1) and employment probabilites in m+12
*===============================================================================
*Generate: outcomes
*gen otherfirm2	= atfirm2==0  & working2==1
*gen nonemp 	= working2==0 & atfirm2==0


eststo   clear
eststo : qui xtreg annual_ear 		$instrument $linear $covariets  if eventtime_def==12 , cluster(varselid) fe nonest
eststo : qui xtreg atfirm2 		$instrument $linear $covariets  if eventtime_def==12 , cluster(varselid) fe nonest
eststo : qui xtreg otherfirm2	 	$instrument $linear $covariets  if eventtime_def==12 , cluster(varselid) fe nonest
eststo : qui xtreg nonemp	 	$instrument $linear $covariets  if eventtime_def==12 , cluster(varselid) fe nonest
esttab , keep($instrument _cons) se(3) b(3) star(* 0.1 ** 0.05 *** 0.01)

esttab using "$agetablepath/RF/tab_ear_and_premp12.tex", 			/// 
replace keep($instrument _cons) booktabs nodepvars nomtitles  			///
varlabels(treat "Long NT" _cons "Control mean") se(3) b(3)  			///
star(* 0.1 ** 0.05 *** 0.01) refcat( _cons "", nolabel)   			///
stats(r2  N_clust N, fmt( %9.3f %9.0g %9.0g) 					/// 
labels( "$ R^2 $"  "No. of Clusters" "N")) 					///
title("\textsc{RD-estimates on earnings and employment} " ) 			///
mlabels("Earnings (t+1)" " $ \Pr(At firm \mid m=12 ) $ " " $ \Pr(Other firm \mid m=12 ) $" " $ \Pr(NE \mid m=12 ) $"    ) 					///
nonotes 


}
*
{ // Cumulative annual earnings after 2 years post notification
*===============================================================================
*Generate Cumulative earnings
bys lopnr (date) : gen cumeartot 	= annual_ear[_n+12] 		+ annual_ear[_n+24] 		if eventtime_def==0 
bys lopnr (date) : gen cumearatfirm2	= annual_ear_atfirm[_n+12] 	+ annual_ear_atfirm[_n+24] 	if eventtime_def==0 
bys lopnr (date) : gen cumearotherfirm2  = annual_ear_otherfirm[_n+12] 	+ annual_ear_otherfirm[_n+24] 	if eventtime_def==0 

eststo   clear
eststo : qui xtreg cumeartot 		$instrument $linear $covariets  if eventtime_def==0 , cluster(varselid) fe nonest
eststo : qui xtreg cumearatfirm2 	$instrument $linear $covariets if eventtime_def==0 , cluster(varselid) fe nonest
eststo : qui xtreg cumearotherfirm2 	$instrument $linear $covariets if eventtime_def==0 , cluster(varselid) fe nonest
esttab , keep($instrument _cons) se(3) b(3) star(* 0.1 ** 0.05 *** 0.01)

esttab using "$agetablepath/RF/tab_cum_annual_ear.tex", 			/// 
replace keep($instrument _cons) booktabs nodepvars nomtitles  			///
varlabels(treat "Long NT" _cons "Control mean") se(2) b(2)  			///
star(* 0.1 ** 0.05 *** 0.01) refcat( _cons "", nolabel)   			///
stats(r2  N_clust N, fmt( %9.3f %9.0g %9.0g) 					/// 
labels( "$ R^2 $"  "No. of Clusters" "N")) 					///
title("\textsc{Cumulative annual earnings 2 years post notification} " ) 	///
mlabels("Total" "Old Firm" "New Firm"    ) 					///
nonotes 

drop cumear*
}
*
replace alosdag=0 if alosdag==.
{ // Cumulative employment probabilites after 2 years post notification
*===============================================================================
*Generate Cumulative earnings

bys lopnr (date) : egen cumatfirm2 	= total(atfirm2==1) 			if inrange(eventtime_def,1,24) 
bys lopnr (date) : egen cumotherfirm2 	= total(atfirm2==0 & working2==1) 	if inrange(eventtime_def,1,24) 
bys lopnr (date) : egen cumnonemployed 	= total(atfirm2==0 & working2==0) 	if inrange(eventtime_def,1,24) 
bys lopnr (date) : egen cumunemployed 	= total(alosdag>0  & working2==0) 	if inrange(eventtime_def,1,24) 
bys lopnr (date) : egen cumoutofLF 	= total(alosdag==0 & working2==0) 	if inrange(eventtime_def,1,24) 


eststo   clear
eststo :  xtreg cumatfirm2 		$instrument $linear $covariets if eventtime_def==1 , cluster(varselid) fe nonest
eststo : qui xtreg cumotherfirm2 	$instrument $linear $covariets if eventtime_def==1 , cluster(varselid) fe nonest
eststo : qui xtreg cumnonemployed 	$instrument $linear $covariets if eventtime_def==1 , cluster(varselid) fe nonest
eststo : qui xtreg cumunemployed 	$instrument $linear $covariets if eventtime_def==1 , cluster(varselid) fe nonest
eststo : qui xtreg cumoutofLF 		$instrument $linear $covariets if eventtime_def==1 , cluster(varselid) fe nonest
esttab , keep($instrument _cons) se(3) b(3) star(* 0.1 ** 0.05 *** 0.01)

esttab using "$agetablepath/RF/tab_cum_employment.tex", 			/// 
replace keep($instrument _cons) booktabs nodepvars nomtitles  			///
varlabels(treat "Long NT" _cons "Control mean") se(3) b(3)  			///
star(* 0.1 ** 0.05 *** 0.01) refcat( _cons "", nolabel)   			///
stats(  N_clust N, fmt( %9.0fc %9.0fc) 					/// 
labels(   "\# clusters" "\# observations")) 					///
title("Cumulated duration (months) in various states, first 2 years after notification " ) 	///
mlabels("At notifying firm" "At new firm" "Non-employed" "Unemployed" "Out of LF" ) 	///
nonotes 

drop cuma* cumo* cumn* cumu* 
}
*

log close
